package com.my.screenadapter.mSharedPreferences;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * Created by zyj on 2016/4/7.
 */
public class SpUtil {

    public static void init(SharedPreferences sp, Context context) {
        if (null == sp) {
            sp = android.preference.PreferenceManager.getDefaultSharedPreferences(context);
        }
    }

    public static boolean writeSpString(SharedPreferences sp, String key,
                                        String value) {
        Editor editor = sp.edit();
        editor.putString(key, value);
        return editor.commit();
    }

    public static int readSpInt(SharedPreferences sp, String key,
                                int defValue) {
        return sp.getInt(key, defValue);
    }

    public static boolean writeSpInt(SharedPreferences sp, String key,
                                     int value) {
        Editor editor = sp.edit();
        editor.putInt(key, value);
        return editor.commit();
    }

    public static boolean writeSpLong(SharedPreferences sp, String key,
                                      long value) {
        Editor editor = sp.edit();
        editor.putLong(key, value);
        return editor.commit();
    }

    public static float readSpFloat(SharedPreferences sp, String key,
                                    float defValue) {
        return sp.getFloat(key, defValue);
    }

    public static boolean writeSpFloat(SharedPreferences sp, String key,
                                       float value) {
        Editor editor = sp.edit();
        editor.putFloat(key, value);
        return editor.commit();
    }


    public static String readSpString(SharedPreferences sp, String key,
                                      String defValue) {
        return sp.getString(key, defValue);
    }

    public static boolean writeSpBoolean(SharedPreferences sp, String key, boolean value) {
        Editor editor = sp.edit();
        editor.putBoolean(key, value);
        return editor.commit();
    }

    public static boolean readSpBoolean(SharedPreferences sp, String key, boolean defValue) {
        return sp.getBoolean(key, defValue);
    }

    public static boolean clearSp(SharedPreferences sp) {
        return sp.edit().clear().commit();
    }
}
